<?php
namespace app\logic\yuql;



use app\services\yuql\GeoServices;
use app\services\yuql\RedisServices;

class GeoLogic
{
    /**
     * yuql
     * 获取地理位置经纬度存入redis
     * @param $params
     * @return int
     * @throws \Exception
     */
    public static function houseAdd($params){
        if (empty($params)) throw new \Exception('添加房源信息不能为空');
        $address = $params['address'];
        $getLongLatByAddress= GeoServices::getLongLatByAddress($address);
        //list($params['longitude'], $params['latitude'])
        $redis = RedisServices::getInstance()->getRedis();
        $redis->select(1);
        $key = '2008a_house';
        $result = $redis->geoadd($key,$getLongLatByAddress[0],$getLongLatByAddress[1],$params['address']);
        return $result;
    }

    /**
     * yuql
     * 搜索附近房源
     * @param $params
     * @throws \Exception
     */
    public static function searchHouse($params){
        if (empty($params)) throw new \Exception('搜索房源信息不能为空');
        $address = $params['addressKey'];
        $getLongLatByAddress= GeoServices::getLongLatByAddress($address);
        $redis = RedisServices::getInstance()->getRedis();
        $key = '2008a_house';
        $redis->select(1);
        $option = array(
            'WITHDIST',  //距离信息
            'WITHCOORD'  //坐标信息
        );
        $result = $redis->geoRadius($key,$getLongLatByAddress[0],$getLongLatByAddress[1],10,'km',$option);
        if (empty($result)) throw new \Exception('附近没有此房源');
        return $result;
    }
}